package interfaces.batch.legacy.importlegacy.jenkins.service.extractor.maven.pom;

import interfaces.batch.legacy.importlegacy.jenkins.domain.jenkins.job.ConfigJob;
import interfaces.batch.legacy.importlegacy.jenkins.service.http.HttpResourceContentFetcher;
import org.springframework.beans.factory.annotation.Required;

import java.util.List;

/**
 * @author Gregory Boissinot
 */
public class POMContentExtractor {

    private HttpResourceContentFetcher httpResourceContentFetcher;

    private List<RemotePOMURLStrategy> strategies;

    @Required
    public void setHttpResourceContentFetcher(HttpResourceContentFetcher httpResourceContentFetcher) {
        this.httpResourceContentFetcher = httpResourceContentFetcher;
    }

    @Required
    public void setStrategies(List<RemotePOMURLStrategy> strategies) {
        this.strategies = strategies;
    }

    public String getPomContent(ConfigJob configJob, Object... objects) {

        RemotePOMURLStrategyRetriever strategyRetriever = new RemotePOMURLStrategyRetriever();
        final StrategyType strategyType = strategyRetriever.getStrategyType(configJob);

        final RemotePOMURLStrategy strategyObject = strategyRetriever.getStrategyObject(strategyType, strategies);
        if (strategyObject == null) {
            return null;
        }

        final String remotePomUrl = strategyObject.getRemotePomURL(configJob, objects);
        if (remotePomUrl != null) {
            return httpResourceContentFetcher.getContent(remotePomUrl);
        }

        return null;
    }
}
